// 
// Copyright 2010 Classpath Pty Ltd <opensource@classpath.com.au>
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// 

**
** This migration provides the initial database configuration required to track further changes and migrations.
**
class MigrationZero : Migration {

    **
    ** The name of the table used for tracking versions.
    **
    const static Str tableName := "version_history"

    **
    ** The name of the column used as an ID.
    **
    const static Str idColumnName := "vh_id"

    **
    ** The name of the column used for timestamps.
    **
    const static Str timestampColumnName := "vh_timestamp"

    **
    ** The name of the column used for tracking the `sys::Version` being migrated from.
    **
    const static Str fromVersionColumnName := "vh_from_version"

    **
    ** The name of the column used for tracking the `sys::Version` being migrated to.
    **
    const static Str toVersionColumnName := "vh_to_version"

    **
    ** The `sys::Version` being migrated from (uses `sys::Version.defVal`).
    **
    override Version from := Version.defVal

    **
    ** The `sys::Version` being migrated to (uses `sys::Version.defVal`).
    **
    override Version to := Version.defVal

    **
    ** Upgrades the database schema by creating the version history table.
    **
    override Void up() {
        Pod.of(this).log.debug("${typeof}.up - start")
        createTable(tableName) {
            createColumn(idColumnName, integer) { autoIncrement; comment("The ID of the migration"); primaryKey }
            createColumn(timestampColumnName, timestamp) { comment("The timestamp of the migration"); defaultValue("NOW()"); notNull }
            createColumn(fromVersionColumnName, varchar(32)) { comment("The version being migrated from"); notNull }
            createColumn(toVersionColumnName, varchar(32)) { comment("The version being migrated to"); notNull }
        }
        Pod.of(this).log.debug("${typeof}.up - end")
    }

    **
    ** Downgrade the database schema by dropping the version history table.
    **
    override Void down() {
        Pod.of(this).log.debug("${typeof}.down - start")
        dropTable(tableName)
        Pod.of(this).log.debug("${typeof}.down - end")
    }

}
